﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ShredTheLog.Api;

namespace ShredTheLog.Model
{
    [XmlRoot("R")]
    public sealed class ContactListResponse : IContactListResponse
    {
        [XmlAttribute("V")]
        public string Value { get; set; }

        [XmlIgnore]
        public ContactListResponseKind Kind
        {
            get
            {
                ContactListResponseKind result;
                if (Enum.TryParse<ContactListResponseKind>(Value, out result))
                    return result;
                else
                    throw new NotSupportedException(String.Format("Unsupported response during contact list related operation: {0}", Value ?? "null"));
            }
        }
    }

    [XmlRoot("R")]
    public sealed class UserSearchResponse : IUserSearchResponse
    {
        /// <summary>
        /// Количество непросмотренных записей включая текущую страницу
        /// </summary>
        /// <remarks>
        /// [Общее число записей] - ([Номер просматриваемой страницы (нумерация с 1)] - 1) * [Количество записей на страницу]
        /// </remarks>
        [XmlAttribute("V")]
        public int RestCount { get; set; }

        private static readonly ShortUserInfo[] EmptyUsers = new ShortUserInfo[0];
        private ShortUserInfo[] _users = EmptyUsers;
        [XmlElement("row")]
        public ShortUserInfo[] Users
        {
            get { return _users; }
            set { _users = value ?? EmptyUsers; }
        }

        IShortUserInfo[] IUserSearchResponse.Users { get { return Users; } }
    }
}
